// pages/index/free/insurance.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    city: '',
    cityid: '',
    proviceid: '',
    landing_page: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.viewinsurance()
  },
  viewinsurance: function (e) {
    var that = this
    wx.request({
      url: app.apiUrl('c=zhaobiao/index&a=view_insurance'),
      data: {},
      method: "GET",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
        //'content-type': 'application/json;charset=utf-8'
      },
      success: function (res) {
        console.log(res.data)
        var landing_page = res.data.datas.landing_page;
        var bind_count = res.data.datas.bid_count
        var bids = bind_count.replace(/\b(0+)/gi, "")
        that.setData({
          landing_page: landing_page,
          count: bids
        })

      },
      fail: function () {
        console.log('请求失败!')
      }
    })

  },
  //城市选择
  choosecity: function () {
    wx.navigateTo({
      url: '../public/custom/cucity'
    })
  },
  settime: function () {
    var that = this
    setTimeout(function () {
      that.setData({
        alertmess: false
      })
    }, 2000)
  },
  //form表单跳转提交
  formSubmit: function (e) {
    var that  = this
    var city = e.detail.value.city
    var aleaname = e.detail.value.rqname
    var area = e.detail.value.area
    var tele = e.detail.value.tele
    var id = 2
    var title = '申请资料提交成功'
    var tsmall = '恭喜您，申请资料成功，我们将尽快与您联系!'
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    var cityid = that.data.cityid
    var proviceid = that.data.proviceid
    var city = that.data.city
    var landing_page = that.data.landing_page
    if (city == '') {
      that.setData({
        alertmess: true,
        message: '请选择城市！'
      })
      that.settime()
      return false;
    } else if (aleaname == '') {
      that.setData({
        alertmess: true,
        message: '请填写小区名称！'
      })
      that.settime()
    } else if (area == '') {
      that.setData({
        alertmess: true,
        message: '请填写房屋面积！'
      })
      that.settime()
    } else if (tele == '') {
      that.setData({
        alertmess: true,
        message: '请输入手机号码！'
      })
      that.settime()
      return false;
    } else if (tele.length != 11) {
      that.setData({
        alertmess: true,
        message: '请重新输入号码！'
      })
      that.settime()
    } else if (!myreg.test(tele)) {
      that.setData({
        alertmess: true,
        message: '手机号错误,请重新输入！'
      })
      that.settime()
    }else {
      wx.request({
        url: app.apiUrl('c=zhaobiao/index&a=submit_design'),
        data: {
          phone: tele,
          address: aleaname,
          acreage: area,
          province: proviceid,
          city: cityid,
          landing_page: landing_page
        },
        method: "POST",
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          var code = res.data.code
          var message = res.data.error
          if (code == 200) {
            wx: wx.navigateTo({
              url: 'applysuccess?data=' + [id, title, tsmall],
            })
          } else if (code == 400) {
            that.setData({
              alertmess: true,
              message: message
            })
            that.settime()
          }
        }
      })
    }

    console.log('form发生了submit事件，携带数据为：', e.detail.value)
  },
  skipTop: function (e) {
    wx.pageScrollTo({
      scrollTop: 0
    })
  }
  
})